package com.gaoyi.crm.adapter.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.viewbinding.ViewBinding;

import java.util.List;

public abstract class BaseWidgetAdapter<V extends ViewBinding, D> extends BaseAdapter {

    protected List<D> mList;
    protected Context mContext;
    protected V mBinding;

    public BaseWidgetAdapter(Context context, List<D> list) {
        this.mList = list;
        this.mContext = context;
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public D getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            mBinding = getViewBinding(LayoutInflater.from(mContext),viewGroup);
            convertView = mBinding.getRoot();
            convertView.setTag(mBinding);
        } else {
            mBinding = (V) convertView.getTag();
        }
        initView(mBinding,getItem(i));
        return convertView;
    }

    protected abstract void initView(V binding,D item);

    protected abstract V getViewBinding(LayoutInflater inflater, ViewGroup viewGroup);

}
